package com.yiyi.web.controller.utils;

import com.alibaba.fastjson.JSON;
import com.yiyi.common.utils.StringUtils;
import com.yiyi.system.domain.ConversionType;
import com.yiyi.system.domain.MarketingBaidu;
import com.yiyi.system.domain.MarketingData;
import com.yiyi.system.mapper.MarketingBaiduMapper;
import com.yiyi.system.mapper.MarketingDataMapper;
import com.yiyi.system.service.IMarketingDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 营销定时任务
 *
 */
@Component("marketingTask")
public class MarketingTask
{
    private static final Logger logger = LoggerFactory.getLogger(MarketingTask.class);
    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    private MarketingDataMapper marketingDataMapper;

    @Autowired
    private MarketingBaiduMapper marketingBaiduMapper;

   @Autowired
   private IMarketingDataService marketingDataService;

   private Random random=new Random();

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }


    /**
     *上传百度营销数据
     */
    public void uploadBaiduMarketingData(Boolean isTest) {
        MarketingData marketingData=new MarketingData();
        MarketingBaidu marketingBaidu=new MarketingBaidu();
        List<MarketingData> marketingDataLists=marketingDataMapper.selectMarketingDataList(marketingData);
        List<MarketingBaidu>  marketingBaiduLists= marketingBaiduMapper.selectMarketingBaiduList(marketingBaidu);
        logger.info("百度回传开始===============");
        //回传数据
        for(int i=0;i<marketingBaiduLists.size();i++){
            //获取回传设置
            MarketingBaidu marketingSet=marketingBaiduLists.get(i);
            String marketingLink=marketingSet.getMarketingLink();
            logger.info("百度回传链接地址为"+marketingLink);
            double ratio=new Double(marketingSet.getReturnRatio());
            //筛选回传数据
            List<MarketingData> dataLists=marketingDataLists.stream().filter(data->data.getDomainUrl().equals(marketingLink)).collect(Collectors.toList());
            Long[] ids=dataLists.stream().mapToLong(MarketingData::getId).boxed().toArray(Long[]::new);
            //数据总数
            double total=Double.valueOf(dataLists.size());
            logger.info(marketingLink+"百度回传中总数为："+total);
            logger.info(marketingLink+"百度回传中回传率为："+ratio);
            //取数
            double getCount=Math.round(total*ratio);
            //
            int count=0;
            if(total>1){
                count=(int)getCount;
            }
            if(total==1){
                count=1;
            }
            logger.info(marketingLink+"百度回传中需要回传的个数为："+count);
            Collections.shuffle(dataLists);
            List<MarketingData> datas=dataLists.subList(0,count);
            for(int j=0;j<datas.size();j++){
                MarketingData data=datas.get(j);
                logger.info(marketingLink+"百度回传开始===============");
                logger.info(marketingLink+"百度回传选择==============="+data.getId());
                String jsonString=data.getData();
                ConversionType returnJson=JSON.parseObject(jsonString, ConversionType.class);
                marketingDataService.ocpc(returnJson);
                logger.info(marketingLink+"百度回传结束===============");
            }
            if(isTest) {
                if(ids.length>0) {
                    marketingDataMapper.deleteMarketingDataByIds(ids);
                }
            }
        }
        logger.info("百度回传结束===============");
    }

}
